import utime
import random
from neopixel import NeoPixel
from machine import Pin
from machine import SoftI2C
from 驱动.ws2812Screen import WS2812Screen
from 驱动.debounce import *
from 驱动.mpu6050 import accel

sda = Pin(40)
scl = Pin(41)
i2c = SoftI2C(sda=sda, scl=scl, freq=40000)

# 第二个参数为加速度最大量程
# 0 ±2G   1  ±4G
# 2 ±8G   3  ±16G
mpu = accel(i2c, 0)

# 定义方向和角度的阈值
THRESHOLD_ANGLE = 0.4

button_L = Debounce(39)
button_OK = Debounce(36)
button_C = Debounce(38)
button_R = Debounce(37)

SCREEN_WIDTH = 32  # 屏幕宽
SCREEN_HEIGHT = 8  # 屏幕高

num_leds = SCREEN_WIDTH * SCREEN_HEIGHT  # 像素数量(led灯珠数量)

pixels = NeoPixel(Pin(14), num_leds + 64)  # 创建像素序列对象

screen = WS2812Screen(pixels, SCREEN_WIDTH, SCREEN_HEIGHT)  # 创建屏幕对象


# 定义贪吃蛇和食物的颜色
SNAKE_COLOR = (0, 200, 0)  # 红色
FOOD_COLOR = (200, 0, 0)  # 绿色

# 初始化贪吃蛇的位置和初始长度
snake = [(16, 4), (15, 4), (14, 4)]
snake_length = 3

# 初始化食物的位置
food = (random.randint(0, SCREEN_WIDTH - 1), random.randint(0, SCREEN_HEIGHT - 1))

# 定义贪吃蛇移动的方向
direction = "RIGHT"

# 游戏循环
while True:
    # 读取传感器数据
    accel_data = mpu.get_values()
    accel_x = round(accel_data['AcX'] / 16384.0, 2)
    accel_y = round(accel_data['AcY'] / 16384.0, 2)

    # 清空屏幕
    screen.fill_screen()

    # 绘制贪吃蛇
    for segment in snake:
        screen.draw_pixel(segment[0], segment[1], SNAKE_COLOR)

    # 绘制食物
    screen.draw_pixel(food[0], food[1], FOOD_COLOR)

    # 刷新屏幕
    screen.show()

    # 等待一段时间
    # 可以根据需要调整游戏速度
    utime.sleep(0.15)

    # 检测按键输入
    # 可以根据具体的硬件平台和引脚配置进行调整
    # if button_OK.is_pressed() and direction != "DOWN":
    #     direction = "UP"
    # elif button_C.is_pressed() and direction != "UP":
    #     direction = "DOWN"
    # elif button_L.is_pressed() and direction != "RIGHT":
    #     direction = "LEFT"
    # elif button_R.is_pressed() and direction != "LEFT":
    #     direction = "RIGHT"

    # 根据传感器数据确定蛇的移动方向
    if abs(accel_x) > THRESHOLD_ANGLE:
        if accel_x > 0 and direction != "LEFT":
            direction = "RIGHT"
        elif accel_x < 0 and direction != "RIGHT":
            direction = "LEFT"
    elif abs(accel_y) > THRESHOLD_ANGLE:
        if accel_y > 0 and direction != "UP":
            direction = "DOWN"
        elif accel_y < 0 and direction != "DOWN":
            direction = "UP"

    # 根据方向移动贪吃蛇的头部
    head = snake[0]
    if direction == "UP":
        new_head = (head[0], head[1] - 1)
    elif direction == "DOWN":
        new_head = (head[0], head[1] + 1)
    elif direction == "LEFT":
        new_head = (head[0] - 1, head[1])
    elif direction == "RIGHT":
        new_head = (head[0] + 1, head[1])

    # 检查是否撞到边界或自身
    if new_head[0] < 0 or new_head[0] >= SCREEN_WIDTH or new_head[1] < 0 or new_head[1] >= SCREEN_HEIGHT or new_head in snake:
        # 游戏结束
        print("GAME OVER")
        break

    # 将新头部加入贪吃蛇
    snake.insert(0, new_head)

    # 检查是否吃到食物
    if new_head == food:
        # 生成新的食物位置
        food = (random.randint(0, SCREEN_WIDTH - 1), random.randint(0, SCREEN_HEIGHT - 1))
        # 增加贪吃蛇的长度
        snake_length += 1

    # 如果贪吃蛇长度超过了指定值，删除尾部
    if len(snake) > snake_length:
        snake.pop()
        